package com.diabeco.ui.measures;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.diabeco.R;
import com.diabeco.data.measures.FoodIntakeMeasureDaoImpl;
import com.diabeco.data.measures.PhysicalExerciseMeasureDaoImpl;
import com.diabeco.model.measures.PhysicalExerciseMeasure;

public class ListPhysicalExerciseMeasuresActivity extends Activity {

	private static final int REQUEST_CODE = 1237;

	private List<PhysicalExerciseMeasure> measures;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_physical_exercise_measures);

		this.setMeasures(PhysicalExerciseMeasureDaoImpl.getInstance(this).selectAll());

		this.initializeMeasuresList();
	}

	public void addPhysicalExerciseMeasureAction(View view) {

		Intent intent = new Intent(this, MeasurePhysicalExerciseActivity.class);
		startActivityForResult(intent, REQUEST_CODE);

	}


	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		this.setMeasures(PhysicalExerciseMeasureDaoImpl.getInstance(this).selectAll());

		this.initializeMeasuresList();

	}
	
	private void initializeMeasuresList() {

		List<PhysicalExerciseMeasure> measures = this.getMeasures();

		if (!measures.isEmpty()) {
			ListView measuresList = (ListView) findViewById(R.id.measures_list);

			ArrayAdapter<PhysicalExerciseMeasure> measuresAdapter = new ArrayAdapter<PhysicalExerciseMeasure>(this,
					android.R.layout.simple_list_item_1, measures);
			measuresList.setAdapter(measuresAdapter);

			measuresList.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

					ArrayAdapter<PhysicalExerciseMeasure> arrayAdapter = (ArrayAdapter<PhysicalExerciseMeasure>) ((ListView) parent)
							.getAdapter();
					PhysicalExerciseMeasure measure = arrayAdapter.getItem(position);

					Intent intent = new Intent(parent.getContext(), MeasurePhysicalExerciseActivity.class);
					intent.putExtra("measureId", measure.getId());
					startActivity(intent);
				}
			});
			
			measuresList.setOnItemLongClickListener(new OnItemLongClickListener() {

	            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

	            	ArrayAdapter<PhysicalExerciseMeasure> arrayAdapter = (ArrayAdapter<PhysicalExerciseMeasure>)((ListView)parent).getAdapter();
	            	PhysicalExerciseMeasure physicalExerciseMeasure = arrayAdapter.getItem(position);

	            	PhysicalExerciseMeasureDaoImpl.getInstance(ListPhysicalExerciseMeasuresActivity.this).delete(physicalExerciseMeasure.getId());
	            	getMeasures().remove(physicalExerciseMeasure);
	            	
	            	arrayAdapter.notifyDataSetChanged();

	                return true;
	            }
	        }); 
		} else {
			// TODO inform the user there are no measures taken
		}
	}

	public List<PhysicalExerciseMeasure> getMeasures() {
		return measures;
	}

	public void setMeasures(List<PhysicalExerciseMeasure> measures) {
		this.measures = measures;
	}

}
